package _12_回溯;

import java.util.ArrayList;
import java.util.List;

public class _77_组合 {
    public static void main(String[] args) {
        _77_组合 v = new _77_组合();
        List<List<Integer>> combine = v.combine(4, 2);
        for (List<Integer> integers : combine) {
            System.out.println(integers);
        }
    }
    private int n;
    private int k;
    public List<List<Integer>> combine(int n, int k) {
        this.n = n;
        this.k = k;
        ArrayList<List<Integer>> results = new ArrayList<>();
        int[] subList = new int[k];
        dfs(results, subList, 1, 0);
        return results;
    }


    public void dfs(List<List<Integer>> results, int[] subList, int begin, int level) {
        if (level == k) {
            ArrayList<Integer> list = new ArrayList<>();
            for (int i : subList) {
                list.add(i);
            }
            results.add(list);
            return;
        }
        for (int i = begin; i <= n; i++) {
            subList[level] = i;
            dfs(results, subList, i + 1, level + 1);
        }

    }

}
